"""
TaskMaster v1 - Main Application
AceFlow v3.0 - Implementation Phase

FastAPI application with user authentication and task management
"""

import os
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.api.auth import router as auth_router
from app.api.tasks import router as tasks_router

# Create FastAPI application
app = FastAPI(
    title="TaskMaster v1",
    description="多用户隔离个人任务管理系统",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
    # Removed redirect_slashes=False to allow automatic trailing slash redirects
)

# Configure CORS - Load from environment variables
cors_origins = os.getenv("CORS_ORIGINS", "http://localhost:3000").split(",")
app.add_middleware(
    CORSMiddleware,
    allow_origins=cors_origins,
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
    allow_headers=["*"],
)

# Include API routers
print("Registering auth router...")
app.include_router(
    auth_router,
    prefix="/api/auth",
    tags=["Authentication"]
)
print("Auth router registered successfully")

print("Registering tasks router...")
app.include_router(
    tasks_router,
    prefix="/api/tasks",
    tags=["Tasks"]
)
print("Tasks router registered successfully")


@app.get("/")
async def root():
    """Root endpoint"""
    return {
        "message": "TaskMaster v1 API",
        "version": "1.0.0",
        "docs": "/docs",
        "status": "running"
    }


@app.get("/health")
async def health_check():
    """Health check endpoint"""
    return {
        "status": "healthy",
        "timestamp": "2025-09-07T14:50:00Z"
    }


if __name__ == "__main__":
    import uvicorn
    uvicorn.run(
        "main:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )
